package com.yhp.dao.impl;

import com.yhp.dao.PrenumDao;
import com.yhp.javabean.Prenum;
import com.yhp.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PrenumImpl extends DruidUtil implements PrenumDao {
    @Override
    //获取点赞对象
    public Prenum getPre() {
        Prenum prenum = new Prenum();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            String sql = "select * from good where id=1";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                prenum.setId(rs.getInt("id"));
                prenum.setCount(rs.getInt("count"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(conn,ps,rs);
        }
        return prenum;
    }
    //更新点赞数
    public void updatePre() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            String sql = "update good set count=count+1 where id=1";
            ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(conn,ps,rs);
        }
    }
}
